<?php
/**
 * Created by PhpStorm.
 * User: Raytine
 * Date: 2019/4/10
 * Time: 23:23
 */

namespace App\Repository;


use App\model\Biaobiao;
use App\model\Coustomer;
use App\model\Project;
use App\model\Source;
use App\model\Xiaofei;
use App\User;


class Baoxiao
{

    public function create_baoxiao(){

       $projects= Project::with('source')->get();
        \DB::table('baobiao')->truncate();
            Xiaofei::orderBy('id','asc')->chunk(100,function($xiaofeis)use ($projects){
            foreach ($xiaofeis as $xiaofei){
                if ($projects->get($xiaofei->project_id)&&$projects->get($xiaofei->project_id)->source->isNotEmpty() ){
                $baobiao=new Biaobiao();
                $baobiao->project_name=$projects->get($xiaofei->project_id)->name;
                $baobiao->source_name=$projects->get($xiaofei->project_id)->source->get($xiaofei->source_id)->name ?? '';
                $baobiao->xiaofei=$xiaofei->xiaofei;
                $baobiao->ruke_num=Coustomer::where('project_id','=',$xiaofei->project_id)
                    ->where('source_id','=',$xiaofei->source_id)
                    ->whereDate('created_at','=',$xiaofei->created_at)
                    ->count();
                $baobiao->xianchang=Coustomer::where('project_id','=',$xiaofei->project_id)
                    ->where('source_id','=',$xiaofei->source_id)
                    ->where('status','!=',Coustomer::No_Follow_Up)
                    ->whereDate('created_at','=',$xiaofei->created_at)
                    ->count();
                $baobiao->chengjiao=Coustomer::where('project_id','=',$xiaofei->project_id)
                    ->where('source_id','=',$xiaofei->source_id)
                    ->where('status','!=',Coustomer::Completed)
                    ->whereDate('created_at','=',$xiaofei->created_at)
                    ->count();
                $baobiao->time=$xiaofei->created_at;
                $baobiao->save();
                }
            }
       });


    }
}